﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.Mvc;
using System.Threading;
using TestSys.Models;

namespace TestSys.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult LogIn(FormCollection form)
        {
            DataManager dataManager = new DataManager();
            string log = form[0];
            string pass = form[1];
            generalinfo currPers = dataManager.GetUser(log);
            //Authentication failed
            if (currPers == null || currPers.Password != pass)
            {
                ViewData.Add("error", "Bad input");
                return View("Index");
            }
            //Authentication successfull
            FormsAuthentication.SetAuthCookie(currPers.Login, false);
            currPers.IsLoggedIn = true;
            dataManager.entity.SaveChanges();
            if (currPers.permissions.Status == "Admin")
            {
                return RedirectToAction("Index", "Admin", null);
            }
            if (currPers.permissions.Status == "User")
            {
                return RedirectToAction("Tests", "User", null);
            }
            //If got this far, than unknown exception and go to login page
            return View("Index");
        }
    }
}
